| Conditions | 20 |
| Paths | 21 |
| Total Lines | 95 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 39 |
| CRAP Score | 20 |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like floatify.js ➔ ... ➔ parse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | 'use strict'; |
||
| 70 | 279 | var parse = function parse(str) { |
|
| 71 | 279 | var string = str; |
|
| 72 | var spacePos; |
||
| 73 | var spaceSplit; |
||
| 74 | var spaceCount; |
||
| 75 | var dotPos; |
||
| 76 | var commaPos; |
||
| 77 | var lDotPos; |
||
| 78 | var lCommaPos; |
||
| 79 | var dotCount; |
||
| 80 | var commaCount; |
||
| 81 | |||
| 82 | 279 | string = string.trim(); |
|
| 83 | |||
| 84 | // 1st dot position |
||
| 85 | 279 | dotPos = string.indexOf('.'); |
|
| 86 | // 1st comma position |
||
| 87 | 279 | commaPos = string.indexOf(','); |
|
| 88 | // 1st space position |
||
| 89 | 279 | spacePos = string.indexOf(' '); |
|
| 90 | |||
| 91 | 279 | if (dotPos + commaPos + spacePos === -3) { |
|
| 92 | // life is good, no separators |
||
| 93 | 18 | return toFloatFormat(string); |
|
| 94 | } |
||
| 95 | |||
| 96 | 261 | spaceSplit = string.split(' '); |
|
| 97 | 261 | spaceCount = spaceSplit.length - 1; |
|
| 98 | 261 | dotCount = string.split('.').length - 1; |
|
| 99 | 261 | commaCount = string.split(',').length - 1; |
|
| 100 | |||
| 101 | // only combination of 2 separators allowed |
||
| 102 | 261 | if (dotCount > 0 && commaCount > 0 && spaceCount > 0) { |
|
| 103 | 1 | return Number.NaN; |
|
| 104 | } |
||
| 105 | |||
| 106 | // if there is any separator (space, comma, dot) found more than once, |
||
| 107 | // all other must not be found more than once |
||
| 108 | 260 | if (dotCount > 1 && (commaCount > 1 || spaceCount > 1)) { |
|
| 109 | 4 | return Number.NaN; |
|
| 110 | } |
||
| 111 | |||
| 112 | 256 | if (commaCount > 1 && spaceCount > 1) { |
|
| 113 | 1 | return Number.NaN; |
|
| 114 | } |
||
| 115 | |||
| 116 | 255 | if (spaceCount > 0) { |
|
| 117 | 72 | if (!string.match(/^(\d{1,3})?(\s\d{3})*([,\.]\d+)?$/)) { |
|
| 118 | 17 | return Number.NaN; |
|
| 119 | } |
||
| 120 | 55 | string = spaceSplit.join(''); |
|
| 121 | } |
||
| 122 | |||
| 123 | 238 | if (dotPos !== -1 && commaPos !== -1) { |
|
| 124 | // format is using dot and comma |
||
| 125 | |||
| 126 | // last dot position |
||
| 127 | 68 | lDotPos = string.lastIndexOf('.'); |
|
| 128 | // last comma position |
||
| 129 | 68 | lCommaPos = string.lastIndexOf(','); |
|
| 130 | |||
| 131 | // order of 1st dot -> comma must be same as last dot -> comma |
||
| 132 | // 123.123.123,123 -> ok 123.123,123.123 -> not ok |
||
| 133 | 68 | if (Math.sign(dotPos - commaPos) !== Math.sign(lDotPos - lCommaPos)) { |
|
| 134 | 3 | return Number.NaN; |
|
| 135 | } |
||
| 136 | |||
| 137 | // check positions to guess the thousands separator |
||
| 138 | 65 | if (dotPos > commaPos) { |
|
| 139 | 57 | if (dotCount > 1) { |
|
| 140 | 1 | return Number.NaN; |
|
| 141 | } |
||
| 142 | // best guess: . is thousands separator and , is decimal point |
||
| 143 | 56 | return toFloatFormat(string, ',', '.'); |
|
| 144 | } |
||
| 145 | |||
| 146 | 8 | if (commaCount > 1) { |
|
| 147 | 1 | return Number.NaN; |
|
| 148 | } |
||
| 149 | // best guess: , is thousands separator and . is decimal point |
||
| 150 | 7 | return toFloatFormat(string, '.', ','); |
|
| 151 | } |
||
| 152 | |||
| 153 | 170 | if (dotPos !== -1) { |
|
| 154 | // only dot(s) in format |
||
| 155 | 85 | return parseParts(string, '.', dotCount); |
|
| 156 | } |
||
| 157 | |||
| 158 | 85 | if (commaPos !== -1) { |
|
| 159 | // only comma(s) in format |
||
| 160 | 80 | return parseParts(string, ',', commaCount); |
|
| 161 | } |
||
| 162 | |||
| 163 | 5 | return toFloatFormat(string); |
|
| 164 | }; |
||
| 165 | |||
| 174 |